﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using VietImage.Models;
using VietImage.Roles;
using System.Windows.Threading;
using System.Threading;

namespace VietImageSoftware
{
    /// <summary>
    /// Interaction logic for MessageForm.xaml
    /// </summary>
    public partial class MessageForm : Window
    {
        private RegisteredUserRole userRole;

        public RegisteredUserRole UserRole
        {
            get { return userRole; }
            set { userRole = value; }
        }

        private List<ErrorItem> errorList;

        public List<ErrorItem> ErrorList
        {
            get { return errorList; }
            set { errorList = value; }
        }

        private Category currentCategory;

        public Category CurrentCategory
        {
            get { return currentCategory; }
            set { currentCategory = value; }
        }

        private VietImage.Models.Image currentImage;

        public VietImage.Models.Image CurrentImage
        {
            get { return currentImage; }
            set { currentImage = value; }
        }

        private Boolean haveOKButton;

        public Boolean HaveOKButton
        {
            get { return haveOKButton; }
            set { haveOKButton = value; }
        }

        private String txtOK;

        public String TxtOK
        {
            get { return txtOK; }
            set { txtOK = value; }
        }
        private String txtCancel;

        public String TxtCancel
        {
            get { return txtCancel; }
            set { txtCancel = value; }
        }
        private String txtMessage;

        public String TxtMessage
        {
            get { return txtMessage; }
            set { txtMessage = value; }
        }

        private String txtTitle;

        public String TxtTitle
        {
            get { return txtTitle; }
            set { txtTitle = value; }
        }

        private Boolean isOK;

        public Boolean IsOK
        {
            get { return isOK; }
            set { isOK = value; }
        }

        private List<Category> parentCategories;

        public List<Category> ParentCategories
        {
            get { return parentCategories; }
            set { parentCategories = value; }
        }

        private List<DetailCategory> detailCategories;

        public List<DetailCategory> DetailCategories
        {
            get { return detailCategories; }
            set { detailCategories = value; }
        }

        private List<DetailImage> detailImages;

        public List<DetailImage> DetailImages
        {
            get { return detailImages; }
            set { detailImages = value; }
        }

        public MessageForm()
        {
            InitializeComponent();
            
        }

        #region Message Box

        private void btnMCancel_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            isOK = false;
            Close();
        }

        private void btnMOK_Click(object sender, System.Windows.RoutedEventArgs e)
        {
                isOK = true;
                if (txbMMessage.Text.Contains("If you delete this category, all image and sub-categories in it will be deleted too! Do you want to delete the category?") 
                    || txbMMessage.Text.Contains("If you delete this category, all images in it will be deleted too! Do you want to delete the category?"))
                {
                    ProgressBarForm form = new ProgressBarForm();
                    form.UserRole = userRole;
                    form.CurrentCategory = currentCategory;
                    form.Id_Command = 1;
                    form.ShowDialog();
                    currentCategory = Category.GetCategory(form.CurrentCategory.ID);
                }
                else if (txbMMessage.Text.Contains("Do you want choose this image to be primary image of the category?"))
                {
                    ProgressBarForm form = new ProgressBarForm();
                    form.UserRole = userRole;
                    form.CurrentCategory = currentCategory;
                    form.Id_Command = 2;
                    form.ShowDialog();
                    currentCategory = Category.GetCategory(form.CurrentCategory.ID);
                }
                else if (txbMMessage.Text.Contains("Do you want to change this image?"))
                {
                    //isSaved = true;
                    //Manage();
                    ProgressBarForm form = new ProgressBarForm();
                    form.UserRole = userRole;
                    form.CurrentImage = currentImage;
                    form.DetailImages = detailImages;
                    form.Id_Command = 3;
                    form.ShowDialog();
                    currentImage = VietImage.Models.Image.GetImage(form.CurrentImage.ID);
                }
                else if (txbMMessage.Text.Contains("Do you want to add the subcategory to these categories?"))
                {
                    //InsertSubCategoryToCategories();
                    ProgressBarForm form = new ProgressBarForm();
                    form.UserRole = userRole;
                    form.CurrentCategory = currentCategory;
                    form.ParentCategories = parentCategories;
                    form.Id_Command = 4;
                    form.ShowDialog();
                    currentCategory = Category.GetCategory(form.CurrentCategory.ID);
                    parentCategories = currentCategory.GetAvailableParentCategories();
                }
                else if (txbMMessage.Text.Contains("Do you want to create this category?"))
                {
                    //InsertSubCategoryToCategories();
                    ProgressBarForm form = new ProgressBarForm();
                    form.UserRole = userRole;
                    form.CurrentCategory = currentCategory;
                    form.DetailCategories = detailCategories;
                    form.Id_Command = 5;
                    form.ShowDialog();
                    currentCategory = Category.GetCategory(form.CurrentCategory.ID);
                    if (currentCategory != null)
                        DetailCategories = currentCategory.GetDetails();
                }
                else if (txbMMessage.Text.Contains("Do you want to edit this category?"))
                {
                    //InsertSubCategoryToCategories();
                    ProgressBarForm form = new ProgressBarForm();
                    form.UserRole = userRole;
                    form.CurrentCategory = currentCategory;
                    form.DetailCategories = detailCategories;
                    form.Id_Command = 6;
                    form.ShowDialog();
                    currentCategory = Category.GetCategory(form.CurrentCategory.ID);
                    if (currentCategory != null)
                        DetailCategories = currentCategory.GetDetails();
                }
                Close();
        }

        //Show all error messages (for web)
        //private String GetErrorList(List<ErrorItem> el)
        //{
        //    String mess = "";
        //    foreach (ErrorItem kvp in el)
        //    {
        //        if (kvp.Key.Contains("ErrorMessage"))
        //            mess += String.Format(kvp.Message + "{0}", Environment.NewLine);
        //        else
        //            mess += String.Format("-" + kvp.Key + " : " + kvp.Message + "{0}", Environment.NewLine);
        //    }
        //    return mess;
        //}

        //Show a error message (for soft)
        private void GetErrorList()
        {
            //Dispatcher.Invoke(DispatcherPriority.Background,
            //    (SendOrPostCallback)delegate
            //    {
                    if (errorList != null)
                    {
                        if (errorList.Count > 0)
                        {
                            btnMOK.Visibility = Visibility.Hidden;
                            btnMCancel.Content = "OK";
                            txbMMessage.Text = "";
                            lblMTitle.Content = "Messages";
                            foreach (ErrorItem kvp in errorList)
                            {
                                if (kvp.Key.Contains("ErrorMessage"))
                                    txbMMessage.Text += String.Format(kvp.Message + "{0}", Environment.NewLine);
                                else
                                    txbMMessage.Text += String.Format("  " + kvp.Key + " : " + kvp.Message + "{0}", Environment.NewLine);
                            }
                        }
                    }
                //}, null);
        }

        private void messageWindow_Loaded(object sender, RoutedEventArgs e)
        {
            txbMMessage.Text = txtMessage;
            if (haveOKButton)
            {
                btnMOK.Visibility = Visibility.Visible;
                btnMOK.Content = txtOK;
            }
            else
                btnMOK.Visibility = Visibility.Hidden;
            btnMCancel.Content = txtCancel;
            lblMTitle.Content = txtTitle;
            if (errorList != null)
                GetErrorList();
        }
        #endregion

        private void gridMessage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                this.DragMove();
            }
            catch (Exception ex)
            {
            }
        }
    }
}
